package com.crud.service;

import com.crud.dao.EmployeeMapper;
import com.crud.pojo.Employee;
import com.crud.pojo.EmployeeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author sc
 * @ClassName EmployeeService
 * @Description class function:
 * @Date 2022/4/6 15:12:30
 **/

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    public void deleteBatch(List<Integer> ids) {
        // TODO Auto-generated method stub
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        //delete from xxx where emp_id in(1,2,3)
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
    }

    public List<Employee> getll(int i) {
        List<Employee> employees = employeeMapper.selectByExampleWithDept(null);
        return employees;
    }

    /**
     * 员工保存
     * @param employee
     */
    public void saveEmp(Employee employee) {
       employeeMapper.insertSelective(employee);
    }

    /**
     * 检验用户名是否存在
     * @param empName
     * @return true:用户名可用 :false 已存在
     */
    public boolean chrckUser(String empName) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);
        return count==0;
    }

    public Employee getEmp(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public void deleteEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }
}
